package com.gy.smartmanagement.process.processdevice;

import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import butterknife.BindView;

import com.gy.smartmanagement.R;
import com.gy.smartmanagement.base.api.bean.ErrorResultBean;
import com.gy.smartmanagement.base.fragment.BaseFragment;

import com.gy.smartmanagement.process.processdevice.fragment.FixFragment;
import com.gy.smartmanagement.process.processdevice.fragment.MaintenanceFragment;
import com.gy.smartmanagement.process.processdevice.fragment.NumberFragment;
import com.gy.smartmanagement.process.processdevice.fragment.ProfileFragment;
import com.gy.smartmanagement.process.processdevice.mvp.DeviceContract;
import com.gy.smartmanagement.utils.ActivityUtils;


/**
 * Created by MVPModule on 11/15/2019 16:36
 */
public class ProcessDeviceFragment extends BaseFragment implements DeviceContract.IView,
    RadioGroup.OnCheckedChangeListener {

  public static final String TAG = "ProcessDeviceFragment";
  private DeviceContract.Presenter mPresenter;
  private Fragment currentFragment, numberFragment, profileFragment,maintenceFragment,fixFragment;

  @BindView(R.id.device_rg)
  RadioGroup device_rg;
  @BindView(R.id.device_container)
  FrameLayout device_container;


  public static ProcessDeviceFragment newInstance() {
    Bundle args = new Bundle();

    ProcessDeviceFragment fragment = new ProcessDeviceFragment();
    fragment.setArguments(args);
    return fragment;
  }

  @Override
  public int getLayoutResId() {
    return R.layout.fragment_device;
  }

  @Override
  public void initialization() {
    device_rg.setOnCheckedChangeListener(this);
    numberFragment = NumberFragment.newInstance();
    profileFragment = ProfileFragment.newInstance();
    maintenceFragment= MaintenanceFragment.newInstance();
    fixFragment= FixFragment.newInstance();
    ActivityUtils.addFragmentToActivity(childManager, numberFragment, R.id.device_container,
        NumberFragment.TAG);
    currentFragment = numberFragment;
  }


  @Override
  public void setPresenter(@NonNull DeviceContract.Presenter presenter) {
    mPresenter = presenter;
  }

  @Override
  public void onLoadFail(ErrorResultBean.ErrorBean errorBean) {
    showToastTip(errorBean.getErrorResult());
  }

  @Override
  public void show() {
    showLoadingWithStatus(childManager, "获取数据中...");
  }

  @Override
  public void hide() {
    hideLoading();
  }

  @Override
  public void onUnBind() {
    mPresenter.unBindPresent();
  }

  @Override
  public void onCheckedChanged(RadioGroup group, int checkedId) {
    switch (checkedId) {
      case R.id.number_rb:
        switchFragment(currentFragment,numberFragment, NumberFragment.TAG);
        break;

      case R.id.profile_rb:
        switchFragment(currentFragment,profileFragment,ProfileFragment.TAG);
        break;

      case R.id.maintain_rb:
        switchFragment(currentFragment,maintenceFragment,MaintenanceFragment.TAG);
        break;

      case R.id.fix_report_rb:

        break;

      case R.id.fix_action_rb:
        switchFragment(currentFragment,fixFragment,FixFragment.TAG);
        break;

      case R.id.exception_rb:

        break;
    }
  }


  private void switchFragment(Fragment from, Fragment to, String tag) {
    if (currentFragment != to) {
      currentFragment = to;
      FragmentTransaction transaction = childManager.beginTransaction();
      if (!to.isAdded()) {    // 先判断是否被add过
        transaction.hide(from).add(R.id.device_container, to, tag).commitAllowingStateLoss(); //
        // 隐藏当前的fragment，add下一个到Activity中
      } else {
        transaction.hide(from).show(to).commitAllowingStateLoss(); // 隐藏当前的fragment，显示下一个
      }
    }
  }

}
